package com.mixes.view;

import static com.mixes.util.Utility.*;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.mixes.R;

/**
 * TODO Auto-generated constructor stub
 * @author Author:
 * @version Revision:
 */
public class TwitterLoginDialog extends BaseDialog {
    private WebView mWebView;

    private WebSettings mWebSettings;

    private Button mCloseBtn;

    private OnWebListener mOnWebListener;

    /**
     * Constructor
     * @param context
     */
    public TwitterLoginDialog(Context context) {
        super(context);
        initialize();
    }

    /**
     * Constructor
     * @param context
     * @param theme
     */
    public TwitterLoginDialog(Context context, int theme) {
        super(context, theme);
        initialize();
    }

    private void initialize() {
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.dialog_twitter_login);
        getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        mWebView = customFindViewById(this, R.id.web_view);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);

                if (url.contains("oauth_verifier") && !url.contains("denied")) {
                    String[] urlParameters = url.split("\\?")[1].split("&");
                    String oauthVerifier = null;
                    if (urlParameters[0].startsWith("oauth_verifier")) {
                        oauthVerifier = urlParameters[0].split("=")[1];
                    } else if (urlParameters[1].startsWith("oauth_verifier")) {
                        oauthVerifier = urlParameters[1].split("=")[1];
                    }
                    dismiss();

                    if (mOnWebListener != null) {
                        mOnWebListener.onGetParameter(oauthVerifier);
                    }
                }
            }
        });

        mWebSettings = mWebView.getSettings();

        mCloseBtn = customFindViewById(this, R.id.close_btn);
        mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public static interface OnWebListener {
        public void onGetParameter(String... urls);
    }

    public void loadUrl(String url) {
        mWebView.loadUrl(url);
    }

    public void setOnWebListener(OnWebListener onWebListener) {
        mOnWebListener = onWebListener;
    }
}
